package optimizers.ganeat.tictactoe;

import java.util.Map;

import optimizers.commons.RunConfig;
import optimizers.ganeat.Constants;
import optimizers.ganeat.GA;
import optimizers.ganeat.Individual;
import optimizers.ganeat.Node;
import optimizers.ganeat.sin.GASin;
import optimizers.ganeat.sin.IndividualSin;
import runconfiguration.TicTacToeConfiguration;
import visualization.GraphDisplayer;

public class TicTacToeGA extends GA {
	
	public TicTacToeGA(RunConfig config){
		super(config);
		this.maxRunTime = 1000;
	}

	@Override
	public Individual getInitialIndividual() {
		TicTacToeIndividual indiv = new TicTacToeIndividual(innovations, nodeNames, -1, 0, config);
		indiv.nodeGenes.add(new Node("w", 25, 10, Node.SIMPLE_SEQUENCE)); // w
		indiv.nodeGenes.add(new Node("v", 25, 10, Node.SIMPLE_SEQUENCE)); // v
		indiv.nodeGenes.add(new Node("E",30, 10, Node.SIMPLE_SEQUENCE));
		indiv.nodeGenes.add(new Node("a",30, 0.1, Node.SIMPLE_SEQUENCE)); // computer
		indiv.nodeGenes.add(new Node("b",30, 0.1, Node.SIMPLE_SEQUENCE)); // human
		indiv.nodeGenes.add(new Node("Iaa", 0, 0,
				Node.INHIBITING_SEQUENCE));
		indiv.nodeGenes.add(new Node("Ibb", 0, 0,
				Node.INHIBITING_SEQUENCE));
		indiv.nodeGenes.add(new Node("IEE", 0, 0,
				Node.INHIBITING_SEQUENCE));
		indiv.addConnection(indiv.getNode("w"), indiv.getNode("w"), true, 7);
		indiv.addConnection(indiv.getNode("v"), indiv.getNode("v"), true, 7);
		indiv.addConnection(indiv.getNode("E"), indiv.getNode("E"), true, 10);
		indiv.addConnection(indiv.getNode("E"), indiv.getNode("Iaa"), true, 20);
		indiv.addConnection(indiv.getNode("E"), indiv.getNode("Ibb"), true, 20);
		indiv.addConnection(indiv.getNode("a"), indiv.getNode("a"), true, 10);
		indiv.addConnection(indiv.getNode("b"), indiv.getNode("b"), true, 10);
		indiv.addConnection(indiv.getNode("b"), indiv.getNode("Iaa"), true, 20);
		indiv.addConnection(indiv.getNode("b"), indiv.getNode("IEE"), true, 20);
		indiv.addConnection(indiv.getNode("a"), indiv.getNode("Ibb"), true, 20);
		indiv.addConnection(indiv.getNode("a"), indiv.getNode("IEE"), true, 20);
		return indiv;
	}

	@Override
	public Individual getNewIndividual(Map<String, Integer> innovations2,
			Map<String, String> nodeNames2, int i, int j) {
		
		return new TicTacToeIndividual(innovations2, nodeNames2, i, j, config);
	}
	
	public static void main(String[] args) throws CloneNotSupportedException {
		GraphDisplayer displayer = new GraphDisplayer("");
		for (int i = 0; i < 50; i++) {
			TicTacToeIndividual indiv = (TicTacToeIndividual) new TicTacToeGA(new TicTacToeConfiguration())
					.getInitialIndividual();
			indiv.getOligoModel();
			indiv.mutate();
			//indiv.evaluateFitness();
			displayer.addGraph(
					indiv.getOligoModel().getVisualization(250, 250), null,
					indiv.printModelInTextArea());
			displayer.display();
		}
	}

	@Override
	public String getProblemName() {
		// TODO Auto-generated method stub
		return "TicTacToe";
	}

}
